package cn.tang.crm.comm;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import cn.tang.crm.database.DBConnectionPool;

public class TypeCache {
	private static boolean inited = false;
	public Map<String, String> roleCacheMap;
	public Map<String, String> clientTypeMap;
	public Map<String, ClientClass> clientClassMap;

	public static TypeCache instance = new TypeCache();

	private TypeCache() {
		init();
	}

	public String getRoleNameByID(String roleID) {
		return roleCacheMap.get(roleID);
	}

	public String getClientTypeNameByID(String clientTypeID) {
		return clientTypeMap.get(clientTypeID);
	}

	public ClientClass getClientClassByID(String clientClassID) {
		return clientClassMap.get(clientClassID);
	}

	public void init() {
		if (!inited) {
			DBConnectionPool connPool = DBConnectionPool.getInstance();
			Connection conn = null;
			Statement stmt = null;
			try {
				conn = connPool.getConnection();
				stmt = conn.createStatement();
				String userRoleSql = "select * from userRole";
				ResultSet rs = stmt.executeQuery(userRoleSql);
				initUserRole(rs);
				if (stmt != null)
					stmt.close();

				stmt = conn.createStatement();
				String clientTypeSql = "select * from clientType";
				rs = stmt.executeQuery(clientTypeSql);
				initClientType(rs);
				if (stmt != null)
					stmt.close();

				stmt = conn.createStatement();
				String clientClassSql = "select * from clientClass";
				rs = stmt.executeQuery(clientClassSql);
				initClientClass(rs);
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				connPool.returnResource(conn, stmt);
			}

		}
	}

	private void initUserRole(ResultSet rs) throws SQLException {
		roleCacheMap = new ConcurrentHashMap<String, String>();
		while (rs.next()) {
			String id = rs.getString("id");
			String name = rs.getString("roleName");
			roleCacheMap.put(id, name);
		}
	}

	private void initClientType(ResultSet rs) throws SQLException {
		clientTypeMap = new ConcurrentHashMap<String, String>();
		while (rs.next()) {
			String id = rs.getString("id");
			String name = rs.getString("clientTypeName");
			clientTypeMap.put(id, name);
		}
	}

	private void initClientClass(ResultSet rs) throws SQLException {
		clientClassMap = new ConcurrentHashMap<String, ClientClass>();
		while (rs.next()) {
			ClientClass clientClass = new ClientClass();
			String id = rs.getString("id");
			String name = rs.getString("clientClassName");
			long miniTrade = rs.getLong("clientClassMiniTrade");
			long maxTrade = rs.getLong("clientClassMaxTrade");
			clientClass.setClientClassName(name);
			clientClass.setMaxTrade(maxTrade);
			clientClass.setMiniTrade(miniTrade);
			clientClassMap.put(id, clientClass);
		}
	}

	static class ClientClass {
		private String clientClassName;
		private long miniTrade;
		private long maxTrade;

		public String getClientClassName() {
			return clientClassName;
		}

		public void setClientClassName(String clientClassName) {
			this.clientClassName = clientClassName;
		}

		public long getMiniTrade() {
			return miniTrade;
		}

		public void setMiniTrade(long miniTrade) {
			this.miniTrade = miniTrade;
		}

		public long getMaxTrade() {
			return maxTrade;
		}

		public void setMaxTrade(long maxTrade) {
			this.maxTrade = maxTrade;
		}
	}
}
